﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FirebirdSql.Data.FirebirdClient;
using System.Configuration;
namespace University
{
    public partial class TUOBJECT : Form
    {

      static public FbConnection con = new FbConnection("User = SYSDBA;" + "Password = masterkey;" +
                                           "Database =D:\\Навчання\\БД\\University_!\\University - копия\\Backup\\University\\UNIVERSITY.fdb;" +
                                            "Post = 3050;" + "Dialect=3;"+
                                            "Charset = NONE");
        static string sqlSelect = "SELECT * FROM OBJECT  order by UNIQUE_NO";
        FbDataAdapter da = new FbDataAdapter(sqlSelect, con);
        DataTable dt = new DataTable();
        public int node_id=0;
        public TreeNode mynode = new TreeNode();
        Main mForm = new Main();
        public int id_class;
        public TUOBJECT()
        {

            InitializeComponent();
          
        }
        
       public TUOBJECT(TreeNode node,ContextMenuStrip s,string com,int id)
       
        {
            id_class = id;
            mynode = node;
            strip = s;
     
            node_id=Convert.ToInt32(mynode.Tag.ToString());
            da.Fill(dt);
            InitializeComponent();
            bindingSource1.DataSource = dt;
            if (com != "Add")
            {
                textBox1.DataBindings.Add("Text", bindingSource1, "UNIQUE_NO");
                textBox2.DataBindings.Add("Text", bindingSource1, "NAME");
                bindingSource1.Filter = "UNIQUE_NO =" + node_id;
                textBox1.Visible = false;
                button2.Visible = false;
                lbel2.Visible = false;
            }
            else 
            {
                lbel2.Visible = true;
                max_id();
                textBox1.Visible = true;
                button1.Visible = false;
            }
          if (com == "Remove") { Remove("OBJECT"); }
          if (com == "Show") { button1.Visible=false; }
        }
        public virtual  void Remove(string name)
        {
       
            OpenCon();
            FbCommand sqldelete = new FbCommand("DELETE FROM " + name + " WHERE UNIQUE_NO = @UNIQUE_NO", con);
            sqldelete.Parameters.AddWithValue("UNIQUE_NO", Convert.ToInt32(mynode.Tag.ToString()));

            sqldelete.ExecuteNonQuery();

            if (name == "OBJECT") { mynode.Remove(); }

          con.Close();
        }
        public void OpenCon()
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
        }
        public void UpdateFild(string table,string fild,string  text)
        {
            OpenCon();
            FbCommand sqlupdate = new FbCommand("UPDATE " + table + " SET " + fild + "=@" + fild + " WHERE UNIQUE_NO = @UNIQUE_NO", con);
            sqlupdate.Parameters.AddWithValue("UNIQUE_NO", Convert.ToInt32(mynode.Tag.ToString()));
            sqlupdate.Parameters.AddWithValue(fild, text);
            sqlupdate.ExecuteNonQuery();
            con.Close();
        }
        public void max_id()
        {
            OpenCon();
            FbCommand max_id = new FbCommand("SELECT max(UNIQUE_NO) from OBJECT ", con);
            FbDataReader rdr = max_id.ExecuteReader();
            rdr.Read();
            int id = Convert.ToInt32(rdr["MAX"].ToString()) + 1;
            textBox1.Text = id.ToString();
        }
        public int current_id()
        {
            OpenCon();
            FbCommand max_id = new FbCommand("SELECT UNIQUE_NO from OBJECT where UNIQUE_NO=" + Convert.ToInt32(mynode.Tag.ToString()), con);
            FbDataReader rdr = max_id.ExecuteReader();
            rdr.Read();
            return Convert.ToInt32(rdr["UNIQUE_NO"].ToString());
          
        }
       public virtual void InsertFild(string klas)
       {
           
         
            FbCommand cmdInsert = new FbCommand("INSERT INTO OBJECT(UNIQUE_NO,MAJOUR,CLASS,NAME) VALUES(@un," + mynode.Tag.ToString() + ","+klas+",@name)", con);
            cmdInsert.Parameters.AddWithValue("un", textBox1.Text);
            cmdInsert.Parameters.AddWithValue("name", textBox2.Text);
            cmdInsert.ExecuteNonQuery();
            con.Close();
          
            this.Close();
           
        }
        
        public virtual void UpdateForm()
        {
            //OpenCon();
            FbConnection con = new FbConnection(mForm.str);
            con.Open();
           
            UpdateFild("OBJECT", "NAME",textBox2.Text);
           
            TreeNode parent = new TreeNode();
            parent = mynode.Parent;
            mynode.Parent.Nodes.Clear();

            foreach (DataRow row in dt.Rows)
            {
                if (Convert.ToInt32(row["MAJOUR"]) == Convert.ToInt32(parent.Tag.ToString()))
                {
                    TreeNode NewNode = new TreeNode(row["NAME"].ToString());
                    NewNode.Tag = row["UNIQUE_NO"].ToString();
                    if (mForm.check(NewNode) == true)
                    {
                        NewNode.Nodes.Add("");
                    }
                    parent.Nodes.Add(NewNode);
                    NewNode.ContextMenuStrip = strip;
                }

            }
            con.Close();
            this.Close();
        }
        

        private void TUOBJECT_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
         
            UpdateForm();
 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            //string ft=mForm.class_id.ToString();
            InsertFild(id_class.ToString());
            dt.Clear();
            da.Fill(dt);
            mynode.Nodes.Clear();
            foreach (DataRow row in dt.Rows)
            {
                if (Convert.ToInt32(row["MAJOUR"]) == Convert.ToInt32(mynode.Tag.ToString()))
                {
                    TreeNode NewNode = new TreeNode(row["NAME"].ToString());
                    NewNode.Tag = row["UNIQUE_NO"].ToString();
                    if (mForm.check(NewNode) == true)
                    {
                        NewNode.Nodes.Add("");
                    }
                    mynode.Nodes.Add(NewNode);
                    NewNode.ContextMenuStrip = strip;
                }

            }
          
            
          
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

      

    }
}